package com.dayuanit.dy16.atm.service.impl;

import com.dayuanit.dy16.atm.dto.FlowDTO;
import com.dayuanit.dy16.atm.service.IRedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Component
public class RedisService4SpringImpl implements IRedisService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public List<FlowDTO> queryTop10Flow(long userId) {
        String key = "atm:flow:top10:" + userId;
        ValueOperations valueOperations = redisTemplate.opsForValue();

        Object o = valueOperations.get(key);
        return null == o ? Collections.EMPTY_LIST : (List<FlowDTO>)o;
    }

    @Override
    public void saveTop10Flow(long userId, List<FlowDTO> flows) {
        String key = "atm:flow:top10:" + userId;
        redisTemplate.opsForValue().set(key, flows);
    }

    @Override
    public void expireKey(String key, int ttl) {
        redisTemplate.expire(key, ttl, TimeUnit.SECONDS);
    }
}
